﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;

namespace JXT.PrimaryKey.Batman.Container
{
    /// <summary>
    /// 容器基类，如果定义新的容器类请从此类继承
    /// </summary>
    public abstract class ContainerBase : IContainer {
        protected ContainerBase(string name) {
            Name = name;
        }

        #region IContainer Members

        /// <summary>
        /// 容器名称
        /// </summary>
        public string Name {
            get;
            private set;
        }

        /// <summary>
        /// 初始化容器,需要在子类重写
        /// </summary>
        public abstract void Initialize();

        /// <summary>
        /// 从容器获取对象，需要在子类重写
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="name">配置名称</param>
        /// <returns></returns>
        public abstract T Resolve<T>(string name = null);

        /// <summary>
        /// 卸载容器对象，需要在子类重写
        /// </summary>
        public abstract void TearDown();

        /// <summary>
        /// 容器开始初始化事件
        /// </summary>
        public event EventHandler<ContainerEventsArgs> Initializing;

        /// <summary>
        /// 容器初始化结束事件
        /// </summary>
        public event EventHandler<ContainerEventsArgs> Initialized;

        /// <summary>
        /// 卸载开始事件
        /// </summary>
        public event EventHandler<ContainerEventsArgs> TearDowning;

        /// <summary>
        /// 卸载结束事件
        /// </summary>
        public event EventHandler<ContainerEventsArgs> TearDowned;

        /// <summary>
        /// 加载错误事件
        /// </summary>
        public event EventHandler<ContainerFailedEventsArgs> ResolveFailed;

        #endregion

        protected virtual void OnInitializing(ContainerEventsArgs e) {
            if (Initializing != null) {
                Initializing(this, e);
            }
        }

        protected virtual void OnInitialized(ContainerEventsArgs e)
        {
            if (Initialized != null) {
                Initialized(this, e);
            }
        }

        protected virtual void OnTearDowning(ContainerEventsArgs e)
        {
            if (TearDowning != null) {
                TearDowning(this, e);
            }
        }

        protected virtual void OnTearDowed(ContainerEventsArgs e)
        {
            if (TearDowned != null) {
                TearDowned(this, e);
            }
        }

        protected virtual void OnResolveFailed(ContainerFailedEventsArgs e)
        {
            if (ResolveFailed != null) {
                ResolveFailed(this, e);
            }
        }

        /// <summary>
        /// 清理资源
        /// </summary>
        public virtual void Dispose()
        {
            TearDown();
        }
    }
}
